<?php
class Helper_General{
	function randomkeys($length, $type=1)
	{
		if( $type == 2){
			$pattern = '1234567890';
		} else {
			$pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ_';
		}
		$rand = strlen($pattern)-1;
		for($i=0; $i<$length; $i++)
		{
			$key .= $pattern{mt_rand(0,$rand)};    //chr(mt_rand(35, 126));
		}

		return $key;
	}

	function userPasswordEncrypt($password)
	{
		return $password;
	}

	function pageURLEncode($arrayParameters)
	{
		$i = 0;
		$url = '';
		foreach($arrayParameters as $key=>$val){
			if( $i != 0){
				$url .= "^";
			}
			$i++;
			$url .= $key."=".$val;
		}

		return urlencode($url);
		// $url;
	}

	function pageURLEncodeForBase($arrayParameters)
	{

		$i = 0;
		$url = '';
		if(!$arrayParameters)
		{
			return $url;
		}
		foreach($arrayParameters as $key=>$val)
		{


			if(($key=='controller')||($key=='method')||($key=='method')||($key=='btnSubmit')||($key==PAGE_URL)||($key=='frmPaginationAdminindex'))
			{
				continue;
			}

			if( $i != 0){
				$url .= "|";
			}
			$i++;
			$url .= $key."=".base64_encode($val);

		}

		return $url;
	}

	function pageURLDecodeForBase($pageURL)
	{

		$x = array();
		$pageURL = explode("|", $pageURL);

		foreach($pageURL as $key=>$val){

			$start = stripos($val,"=");
			$pKey = substr($val,0,$start);
			$pValue = substr($val,$start+1);
			$x[$pKey] = $pValue;
			//$x = array_merge($x, array($single[0] => $single[1]));
		}

		return $x;
	}

	function pageURLDecode($pageURL)
	{

		$pageURL = urldecode($pageURL);
		$x = array();

		$pageURL = explode("^", $pageURL);
		foreach($pageURL as $key=>$val){
			$single = explode("=", $val);
			if($single[1]!='')
			{
				$x = array_merge($x, array($single[0]=>$single[1]));
			}
		}

		return $x;
	}

	function ratingFormat($rating)
	{
	//	die(var_dumP(number_format($rating, 1, '.', '')));
		return round($rating, 1);
	}

	function creditFormat($rating)
	{
		return round($rating, 2);
	}

	function payFormat($rating){
		return round($rating, COOKIE_CURRENCY_DECIMAL);
	}

	function amountFormat($rating, $currency_id=0, $ShowSymbol=false)
	{
		if( $currency_id == "0" ){
			$currency_id = COOKIE_CURRENCY_VALUE;
		}
		foreach($_SESSION["CURRENCY"] as $obj_Currency){
			if($obj_Currency->id == $currency_id){
				$Decimal = $obj_Currency->Decimal;
				$DecimalPoint = $obj_Currency->DecimalPoint;
				$ThousandSeparator = $obj_Currency->ThousandSeparator;
				$Symbol_right = $obj_Currency->Symbol;
				$Symbol_left = $obj_Currency->SymbolLeft;
				break;
			}
		}

		if( $Symbol_left != "" ){
			$Symbol_left .= " ";
		} else {
			$Symbol_right = " ".$Symbol_right;
		}
		//echo $Decimal.'_'.$DecimalPoint.'_'.$ThousandSeparator;
		$value = number_Format($rating, $Decimal, $DecimalPoint, $ThousandSeparator);
		if( $ShowSymbol ){
			return $Symbol_left.$value.$Symbol_right;
		}
		return $value;
	}

	function orderFee( $obj_Order, $RoleType="H")
	{
		if( $RoleType == "H") {
			return $obj_Order->AmountFinal;
		}
	}

	function creditRateFormat($rating)
	{
		return round($rating, 2);
	}

	function moneyFormat($rating)
	{
		return round($rating, COOKIE_CURRENCY_DECIMAL);
	}
	//用转义字符替换掉特殊字符
	public function chanageChars($str)
	{
		$patterns=array("&",
						"<",
						">",
						" ",
						"'",
						'"'
		);
		$replacements= array(
							 "&amp;",
							 "&lt;",
							 "&gt;",
							 "&nbsp;",
							 "&acute;",
							 "&quot;"
		);
		return str_replace($patterns,$replacements,$str);
	}
	//替换掉所有的特殊字符
	public function specialChars($str, $replace='-',$user_id='')
	{
		$str = $this->specialStringChars($str,$replace);
		return $str."_".$user_id;
	}

	public function Avatar($avatar, $language=1)
	{
		if(  $avatar == "default.png"){
			return $language."_".$avatar;
		} else {
			return $avatar;
		}
	}
	public function specialStringChars($str,$replace="-")
	{
		$replace_template = "@m@g@p@";
		$str_arr = str_split($str);
		print_r($str_arr);
		$r="`~!@#$%^&*()+=|\/{}':;',[].<>?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？ ";
		$len = count($str_arr);
		for($i=0; $i<$len; $i++){
			if( strpos($r,$str_arr[$i])>0 ){
				$str_arr[$i] = $replace_template;
			}
		}
		$str = trim(implode($str_arr, ""));
		$str = preg_replace("/".$replace_template."/", $replace, $str);
		return $str;
	}

	/**find and remove**/
	public function redirect($url)
	{
		header("location:".$url);
		die("");
	}

	//弹出对话框并转入相应页面
	public function jsAlert($Message, $URL="", $IsDied=true)
	{
		$temp = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
			<title></title>
			<link rel="stylesheet" type="text/css" href="/application/style/style.css" />
			<link rel="stylesheet" type="text/css" href="/application/css/jquery_ui/lightness.css" />
			<script type="text/javascript" src="/application/js/jquery.js"></script>
			<script type="text/javascript" src="/common/js/show/jqueryExtension"></script>
			<script type="text/javascript" src="/application/js/jquery-ui-1.8.7.custum.min.js"></script>
		</head>
		<body>
		<script type="text/javascript">'.chr(13).chr(10);
		if( $Message != ""){
			$temp .= '	alert("'.$Message.'");'.chr(13).chr(10);
		}

		if( $URL=="" ){
			$temp .= '	history.back();'.chr(13).chr(10);
		} else {
			$temp .= '	window.location.href="'.$URL.'";'.chr(13).chr(10);
		}

		$temp .= '</script>
		</body>
		</html>'.chr(13).chr(10);

		if( $IsDied == true){
			die( $temp );
		} else {
			echo( $temp );
		}
	}

	public function jsAlertContent($Message, $Content="", $IsDied=true)
	{
		$temp = '<script type="text/javascript">'.chr(13).chr(10);
		if( $Message != ""){
			$temp .= '	alert("'.$Message.'");'.chr(13).chr(10);
		}
		$temp .= $Content.chr(13).chr(10);
		$temp .= '</script>'.chr(13).chr(10);


			echo( $temp );

	}
	
	public function jsConfirm($Message, $YesURL, $NoURL="",$IsDied=true)
	{
			$temp = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				<title></title>
				<link rel="stylesheet" type="text/css" href="/application/style/style.css" />
				<link rel="stylesheet" type="text/css" href="/application/css/jquery_ui/lightness.css" />
				<script type="text/javascript" src="/application/js/jquery.js"></script>
				<script type="text/javascript" src="/common/js/show/jqueryExtension"></script>
				<script type="text/javascript" src="/application/js/jquery-ui-1.8.7.custum.min.js"></script>
			</head>
			<body>
			<script type="text/javascript">'.chr(13).chr(10);
		if( $Message != ""||$YesURL!=""){
			$temp .= '	if(confirm("'.$Message.'")){'.chr(13).chr(10);
			$temp .= '	window.location.href="'.$YesURL.'";}else {'.chr(13).chr(10);
		}
		if($NoURL==""){
			$temp .= '	history.back();'.chr(13).chr(10);
		}else{
			$temp .= '	window.location.href="'.$NoURL.'";'.chr(13).chr(10);
		}

		$temp .= '}</script>
		</body>
		</html>'.chr(13).chr(10);

		if( $IsDied == true){
			die( $temp );
		} else {
			echo( $temp );
		}
	}
	public function hmtlAlert($msg, $URL="", $IsDied=true)
	{
		//
		$language = new Language();
		$temp = "<div style='font-size:12px;font-family:verdana;line-height:180%;color:#000;border:dashed 1px #ccc;padding:10px;margin:20px;'>";
		$temp .= "<span style='color: #d69c00;font-weight:bold'>".$language->_r("Information").": </span>";
		$temp .= $msg;

		if( $URL=="" && $URL != false){
			$temp .= '	<br /><a href="#" onclick="history.back();">'.$language->_r("Error").'</a>'.chr(13).chr(10);
		} else if($URL != false ){
			$temp .= '	<br /><a href="'.$URL.'">'.$language->_r("Go To").'</a>'.chr(13).chr(10);
		}
		$errmsg.="</div>";

		if( $IsDied == true){
			die( $temp );
		} else {
			echo( $temp );
		}
	}

	function HTMLtoText($str)
	{
		return preg_replace("/<.*?>/","",$str);
	}

	//显示时屏蔽网址
	function removeWebsite($str)
	{
		return preg_replace("/(^\W)?\S+\.(com|net|org|gov|edu|dk|uk|cn|jp|kr|us|cc|tv|tm|cd|biz|info|name|mobi|hk|tw|ac|io|sh|ws|in|my|it)(^\W)?/U", "[website forbidden]",$str);
	}

	//按照真实的字符生成数组
	function utf8_StringToArray($str)
	{
		$arr_result = array();
		$start_position = strlen($str);
		$end_position = strlen($str);
		$end_position_changed = false;
		$end_position_template = 0;

		$word_start = 0;
		$word_end = 0;
		//die(var_dump(strlen($str)));
		for($i = 0; $i < strlen($str); $i++){
			if($start_position > $i){
				$start_position = $i;
			}

			if($end_position_changed == false) {
				$end_position = $i;
				$end_position_changed = true;
				//break;
			}
			//var_dump( $str[$i]);
			$value = ord($str[$i]);

			if($value > 127){
				if($value >= 192 && $value <= 223){
					$arr_result[] = substr($str, $i, 2);
					$i++;
				} elseif ($value >= 224 && $value <= 239){
					$arr_result[] = substr($str, $i, 3);
					$i = $i + 2;
				} elseif ($value >= 240 && $value <= 247){
					$arr_result[] = substr($str, $i, 4);
					$i = $i + 3;
				} else {
					echo 'z3';
					echo '<script> alert("'.__("Not a UTF-8 compatible string").'"); history.back();</script>';
					exit();
				}
			} else {
				$arr_result[] = substr($str, $i, 1);
			}
		}
		//var_dump($arr_result);
		//die('');
		return implode("", $arr_result);
	}

	//取出特定长度的字符串，并以一定字符结尾
	function substr_utf8($str, $length, $length_more=3, $plus_text='...', $position=0)
	{
		$start_position = strlen($str);
		$start_byte = 0;
		$end_position = strlen($str);
		$end_position_changed = false;
		$end_position_template = 0;
		$count = 0;
		for($i = 0; $i < strlen($str); $i++){
			if($count >= $position && $start_position > $i){
				$start_position = $i;
				$start_byte = $count;
			}
			$temp = $count-$start_byte;
		//	die(var_dump($temp).$i.']<br />');
			//die('a');
			if(($count-$start_byte) >= $length) {
				$end_position_template = $i;
				break;
			}
			if(($count-$start_byte)>=$length-$length_more && $end_position_changed == false) {
				$end_position = $i;
				$end_position_changed = true;
				//break;
			}
			$value = ord($str[$i]);
			if($value > 127){
				$count++;
				if($value >= 192 && $value <= 223) $i++;
				elseif($value >= 224 && $value <= 239) $i = $i + 2;
				elseif($value >= 240 && $value <= 247) $i = $i + 3;
				else
				{
					echo '<script> alert("'.__("Not a UTF-8 compatible string").'"); history.back();</script>';
					exit();
				}
			}
			$count++;
		}
		//die(var_dump($end_position_template).']');
		if( $end_position_template == 0 || $end_position_template == strlen($str) ){
			//die(var_dump(strlen($str)).']]]');
			if( $end_position_template != 0){
				$end_position = $end_position_template;
			} else {
				$end_position = strlen($str);
			}
			//die(var_dump($end_position).']');
			$plus_text = "";
		} else {
		//die("bbb");
		}
		//die(var_dump($plus_text));
		return(substr($str, $start_position, $end_position-$start_position).$plus_text);
	}

	function createMonthTable($startYear, $startMonth, $endYear, $endMonth){
		$sql = "";
		$Months = "";
		$start_month = 1;
		$end_month = 12;

		for($i=$startYear; $i<=$endYear; $i++){
			$Months = "";

			if($i == $endYear){
				if($i != $startYear){
					$start_month = 1;
				} else {
					$start_month = $startMonth;
				}
				$end_month = $endMonth;
			} else if( $i == $startYear ){
				$start_month = $startMonth;
				$end_month = 12;
			} else {
				$start_month = 1;
				$end_month = 12;
			}

			for($j=$start_month; $j<=$end_month; $j++){
				if( $Months != "" ){
					$Months .= ",".$j;
				} else {
					$Months = $j;
				}
			}
			if( $sql != ""){
				$sql .= "
				union
				select '".$i."', Month from system_month where month in($Months)";
			} else {
				$sql = "
				select '".$i."' as Year, Month from system_month where month in($Months)";
			}
		}

		return $sql;
	}

	function originalAmount($Money, $VATRate, $round_number = 2)
	{
		return round($Money/(1+$VATRate), $round_number);
	}

	function getDateNumber($startDate, $endDate)
	{
		return round((strtotime($endDate) - strtotime($startDate))/3600/24);
	}

	function getDate($date, $number)
	{
		// return strtotime($endDate);
	}

	function compareDate($startDate, $endDate)
	{
		if( $this->isDate($endDate) ==false){
			return -1;
		} else if( strtotime($startDate) == strtotime($endDate) ){
			return 0;
		} else if( strtotime($startDate) > strtotime($endDate) ){
			return 1;
		} else {
			return -1;
		}
	}

	function htmlComment($s)
	{
		echo '<!--This is a comment:';
		print_r($s);
		echo '-->';
	}
}

/*
//function randomkeys()
$obj = new Helper_General();
echo $result = $obj->randomkeys(8);
*/
?>